
<template>
    <div class="container">
        <div class="login">
           <!-- <span>用户名</span><input type="text" name="username">
           <br>
           <span>密码</span><input type="text" name="password"> -->
           <el-form  label-width="0px" class="haode" :model="loginForm"  :rules="rules" ref="loginRef">
                <el-form-item  prop="name">
                    <el-input prefix-icon="el-icon-user"  v-model="loginForm.username"></el-input>
                </el-form-item>
                <el-form-item  prop="pass"  >
                    <el-input   prefix-icon="el-icon-star-off" v-model="loginForm.password"></el-input>
                </el-form-item>

                <el-form-item class="nihao">
                    <el-button  @click="login">登录</el-button>
                    <el-button type="info" @click="resetRef"> 重置</el-button>
                    
                </el-form-item>
            </el-form>
            
        </div>
    </div>
</template>
<script>
import axios from 'axios'
export default {
  data() {
    return {
      rules: {},
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetRef() {
      // console.log(this);
      // 重置表单
      this.$refs.loginRef.resetFields()
    },
    login() {
      // this.$http.post('/login',this.loginForm).then(result=>{
      //     console.log(result.data);
      // })
      // 查看表单的验证是否合法
      console.log(this.loginForm)
      this.$refs.loginRef.validate(async valid => {
        if (!valid) return
       
       const { data: res } = await this.$http.post('login', this.loginForm)
        // console.log(res)
        if (res.meta.status == 200) {
          console.log('登录成功')
          // 将token保存在sessionStorage当中
           window.sessionStorage.setItem('token', res.data.token)
           this.$router.push('/Home')
        } else {
          console.log('登录失败')
          console.log(res)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  background-color: #2b4b6b;
  height: 100%;
  .login {
    height: 400px;
    width: 500px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
}
.nihao {
  display: flex;
  justify-content: flex-end;
}
.haode {
  position: absolute;
  bottom: 30px;
  width: 100%;
}
</style>
